package com.yyfax.pay.order.controller.api;

import java.util.Enumeration;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.WebUtils;

import com.yyfax.framework.common.Log;
import com.yyfax.framework.common.OpResponse;
import com.yyfax.pay.common.annotation.CheckSign;
import com.yyfax.pay.common.model.to.YYPayApiReq2;
import com.yyfax.pay.order.data.service.ProtocolBindService;
import com.yyfax.pay.order.data.service.ProtocolPayService;
import com.yyfax.pay.order.data.service.ProtocolUntieService;
import com.yyfax.pay.third.model.to.fuiou.ProtocolPayNotifyReq;
import com.yyfax.pay.vo.order.*;

/**
 * 富友协议支付api
 * 
 * @author wangjwy
 * @date 2018/6/5 10:19
 * @since 2.2.0
 */
@RestController
public class ProtocolPayApiController {

	@Resource
	private ProtocolBindService protocolBindService;

	@Resource
	private ProtocolPayService protocolPayService;

	@Resource
	private ProtocolUntieService protocolUntieService;

	protected Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 协议绑卡发送验证码
	 * 
	 * @author wangjwy
	 * @date 2018/6/5 14:35
	 * @since 1.0.0
	 * @param req
	 * @return com.yyfax.framework.common.OpResponse
	 * @throws Exception
	 */
	@PostMapping(value = "/api/protocol/send_message")
	@CheckSign
	public OpResponse protocolBindSendMsg(@RequestBody @Valid YYPayApiReq2<ProtocolBindSendMsgReq> req) {
		ProtocolBindSendMsgReq params = req.getParams();
		params.checkValue();
		return protocolBindService.protocolBindSendMsg(params, req.getMerchantCode());
	}

	/**
	 * 富友协议支付绑卡
	 * 
	 * @author wangjwy
	 * @date 2018/6/15 14:10
	 * @since 2.2.0
	 * @param req
	 * @return com.yyfax.framework.common.OpResponse
	 * @throws Exception
	 */
	@CheckSign
	@PostMapping(value = "/api/protocol/binding")
	public OpResponse protocolBind(@RequestBody @Valid YYPayApiReq2<ProtocolBindReq> req) {
		ProtocolBindReq params = req.getParams();
		return protocolBindService.protocolBind(params, req.getMerchantCode());
	}

	/**
	 * 富友协议支付
	 * 
	 * @author wangjwy
	 * @date 2018/6/15 14:11
	 * @since 2.2.0
	 * @param req
	 * @return com.yyfax.framework.common.OpResponse
	 * @throws Exception
	 */
	@PostMapping("/api/protocol/pay")
	@CheckSign
	public OpResponse protocolPay(@RequestBody @Valid YYPayApiReq2<ProtocolPayReq> req) {
		final String OP = "ProtocolPayApiController.protocolPay";
		ProtocolPayReq params = req.getParams();
		params.checkValue();
		return protocolPayService.pay(params, req.getMerchantCode());
	}

	/**
	 * 富友协议支付返回异步通知入口
	 * 第三方根据http返回码是否是200判断
	 * 
	 * @author wangjwy
	 * @date 2018/6/8 14:08
	 * @since 2.2.0
	 * @param req
	 * @return void
	 * @throws Exception
	 */
	@PostMapping(value = "/api/protocol/pay/result/notify")
	public void protocolPayNotify(HttpServletRequest req) {
		final String OP = "ProtocolPayApiController.protocolPayNotify";
		ProtocolPayNotifyReq reqq = buildRepParam(req);
		logger.info(Log.op(OP).msg("富友协议异步返回").kv("req", reqq).toString());
		protocolPayService.handNotifyResult(reqq);
	}

	/**
	 * 协议支付结果查询
	 * 
	 * @author wangjwy
	 * @date 2018/6/8 19:47
	 * @since 2.2.0
	 * @param req
	 * @return void
	 * @throws Exception
	 */
	@PostMapping(value = "/api/protocol/checkResult")
	@CheckSign
	public OpResponse protocolPayCheckResult(@RequestBody @Valid YYPayApiReq2<ProtocolPayQueryReq> req) {
		return protocolPayService.checkResult(req.getParams(), req.getMerchantCode());
	}

	/**
	 * 查询协议卡状态
	 * 
	 * @author wangjwy
	 * @date 2018/6/15 14:51
	 * @since 2.2.0
	 * @param req
	 * @return com.yyfax.framework.common.OpResponse
	 * @throws Exception
	 */
	@CheckSign
	@PostMapping(value = "/api/protocol/bindingQuery")
	public OpResponse protocolPayQueryCard(@RequestBody @Valid YYPayApiReq2<ProtocolCardQueryReq> req) {
		return protocolBindService.queryProtocolCard(req.getParams(), req.getMerchantCode());
	}

	/**
	 * 协议卡解绑
	 * 
	 * @author wangjwy
	 * @date 2018/6/15 14:52
	 * @since 2.2.0
	 * @param req
	 * @return com.yyfax.framework.common.OpResponse
	 * @throws Exception
	 */
	@CheckSign
	@PostMapping(value = "/api/protocol/unbind")
	public OpResponse protocolPayUnBind(@RequestBody @Valid YYPayApiReq2<ProtocolPayUnBindReq> req) {
		return protocolUntieService.unBind(req.getParams(), req.getMerchantCode());
	}

	/**
	 * 构建异步通知请求
	 * 
	 * @author wangjwy
	 * @date 2018/6/19 11:30
	 * @since 2.2.0
	 * @param req
	 * @return com.yyfax.pay.third.model.to.fuiou.ProtocolPayNotifyReq
	 * @throws Exception
	 */
	private ProtocolPayNotifyReq buildRepParam(HttpServletRequest req) {
		ProtocolPayNotifyReq rq = new ProtocolPayNotifyReq();
		if (null != req.getParameter("AMT")) {
			rq.setAmt(Long.valueOf(req.getParameter("AMT")));
		}
		rq.setBankcard(req.getParameter("BANKCARD"));
		rq.setMchntcd(req.getParameter("MCHNTCD"));
		rq.setMchntorderid(req.getParameter("MCHNTORDERID"));
		rq.setOrderid(req.getParameter("ORDERID"));
		rq.setProtocolno(req.getParameter("PROTOCOLNO"));
		rq.setResponsecode(req.getParameter("RESPONSECODE"));
		rq.setResponsemsg(req.getParameter("RESPONSEMSG"));
		rq.setSign(req.getParameter("SIGN"));
		rq.setType(req.getParameter("TYPE"));
		rq.setUserid(req.getParameter("USERID"));
		rq.setVersion(req.getParameter("VERSION"));
		return rq;
	}

}
